<?php
/**
 * 发热病人列表
 * Created by PhpStorm.
 * User: zhang-hong
 * Date: 2017/3/7
 * Time: 13:08
 */
class GetfarelistController extends Yaf_Controller_Abstract
{

    function IndexAction()
    {

        //初始化
        $request = $this->getRequest();
        $user_department = $request->getPost("user_department","");
        $user_department_id = $request->getPost("user_department_id",0);
        $yiyuan_id = $request->getPost("yiyuan_id",0);

        $result["state"] = "1";
        $result["response_info"] = "数据同步成功:)";
        $PatientBasicModel = new PatientBasicInfoModel();
        if(!ctype_digit("$user_department_id"))
        {
            Utility::apiJsonError(20029, '科室ID必须是整型');
        }
        if(!ctype_digit("$yiyuan_id"))
        {
            Utility::apiJsonError(20027, '医院ID必须是整型');
        }
        //获取发烧列表
        $zhuyuan_basic_info = $PatientBasicModel->getfarelist($yiyuan_id,$user_department);
        $zhuyuan_info = array();
        $zhuayun_bingren = array();
        foreach($zhuyuan_basic_info as $one)
        {
            if(!in_array($one['zhuyuan_id'],$zhuayun_bingren))
            {
                $zhuayun_bingren[] = $one['zhuyuan_id'];
                $zhuyuan_info[$one['zhuyuan_id']]['xingming'] = $one['xingming'];
                $zhuyuan_info[$one['zhuyuan_id']]['zhuyuan_id'] = $one['zhuyuan_id'];
                $zhuyuan_info[$one['zhuyuan_id']]['nianling'] = $one['nianling'];
                $zhuyuan_info[$one['zhuyuan_id']]['xingbie'] = $one['xingbie'];
                $zhuyuan_info[$one['zhuyuan_id']]['bingchuang_hao'] = $one['bingchuang_hao'];
            }
        }

        $fare_info = DB::select()
            ->from("zhuyuan_fare")
            ->where("zhuyuan_id","in",$zhuayun_bingren)
            ->and_where("fare_level",">",0)
            ->and_where("temperature",">=",Yaconf::get("config$yiyuan_id.fere_temperature"))
            ->execute()
            ->as_array();
        $data = array();
        if($fare_info){
            if(Yaconf::get("config$yiyuan_id.fenbiao_state")== "true")
            {
                $zhuyuan_tizheng = "zhuyuan_tizheng_".$user_department_id;
            }
            else
            {
                $zhuyuan_tizheng = "zhuyuan_tizheng";
            }
            $i=0;

            foreach($fare_info as $v){
                $data[$i]['zhuyuan_id'] = $v['zhuyuan_id'];
                $data[$i]['fare_level'] = $v['fare_level'];
                $data[$i]['last_temperature'] = $v['temperature'];
                $data[$i]['zhuyuan_id'] = $zhuyuan_info[$v['zhuyuan_id']]['zhuyuan_id'];
                $data[$i]['xingming'] = $zhuyuan_info[$v['zhuyuan_id']]['xingming'];
                $data[$i]['nianling'] = $zhuyuan_info[$v['zhuyuan_id']]['nianling'];
                $data[$i]['xingbie'] = $zhuyuan_info[$v['zhuyuan_id']]['xingbie'];
                $data[$i]['bingchuang_hao'] = $zhuyuan_info[$v['zhuyuan_id']]['bingchuang_hao'];
                //最后一次体温测量结果
                $last_shuju = DB::select()
                    ->from($zhuyuan_tizheng)
                    ->where("zhuyuan_id","=",$v['zhuyuan_id'])
                    ->and_where("jiancha_type","=","体温")
                    ->and_where("jiancha_fangshi","=","腋温")
                    ->and_where("jiancha_time",">=",date("Y-m-d H:i",strtotime("-72 hour")))
                    ->order_by("jiancha_value","DESC")
                    ->execute()
                    ->offsetGet(0);
                if($last_shuju){
                    $data[$i]['high_temperature'] = $last_shuju['jiancha_value'];
                    $data[$i]['jiancha_time'] = $last_shuju['jiancha_time'];
                }else{
                    $data[$i]['last_temperature'] = $v['temperature'];
                    $data[$i]['high_temperature'] = "";
                    $data[$i]['jiancha_time'] = $v['jiancha_time'];
                }
                $i++;
            }
        }
        $result["result"] = $data;
        $result["error"] = 200;
        $result["msg"] = "";
        Utility::apiJsonResultExtends($result);
    }
//    function Index1Action()
//    {
//        //初始化
//        $request = $this->getRequest();
//        $user_department = $request->getPost("user_department","");
//        $user_department_id = $request->getPost("user_department_id",0);
//        $yiyuan_id = $request->getPost("yiyuan_id",0);
//
//        $result["state"] = "1";
//        $result["response_info"] = "数据同步成功:)";
//        $PatientBasicModel = new PatientBasicInfoModel();
//        if(!ctype_digit("$user_department_id"))
//        {
//            Utility::apiJsonError(20029, '科室ID必须是整型');
//        }
//        if(!ctype_digit("$yiyuan_id"))
//        {
//            Utility::apiJsonError(20027, '医院ID必须是整型');
//        }
//        //获取发烧列表
//        $zhuyuan_basic_info = $PatientBasicModel->getfarelist($yiyuan_id,$user_department);
//        //print_r($zhuyuan_basic_info);exit;
//        $zhuyuan_info = array();
//        $zhuayun_bingren = array();
//        foreach($zhuyuan_basic_info as $one)
//        {
//            if(!in_array($one['zhuyuan_id'],$zhuayun_bingren))
//            {
//                $zhuayun_bingren[] = $one['zhuyuan_id'];
//                $zhuyuan_info[$one['zhuyuan_id']]['xingming'] = $one['xingming'];
//                $zhuyuan_info[$one['zhuyuan_id']]['zhuyuan_id'] = $one['zhuyuan_id'];
//                $zhuyuan_info[$one['zhuyuan_id']]['nianling'] = $one['nianling'];
//                $zhuyuan_info[$one['zhuyuan_id']]['xingbie'] = $one['xingbie'];
//                $zhuyuan_info[$one['zhuyuan_id']]['bingchuang_hao'] = $one['bingchuang_hao'];
//            }
//        }
//        //print_r($zhuayun_bingren);exit;
//        //获取当前所有病人状态
//        //$zhuayun_bingren = array(1,2);
//        $fare_info = array();
//        if(is_array($zhuayun_bingren)&&count($zhuayun_bingren)> 0)
//        {
//            $fare_info = $PatientBasicModel->getAllpatientStatus($zhuayun_bingren,$yiyuan_id);
//        }else
//        {
//            Utility::apiJsonError(20012, '输入数据有误!');
//        }
//        $data = array();
//        if($fare_info){
//            if(Yaconf::get("config$yiyuan_id.fenbiao_state") == "true")
//            {
//                $zhuyuan_tizheng = "zhuyuan_tizheng_".$user_department_id;
//            }
//            else
//            {
//                $zhuyuan_tizheng = "zhuyuan_tizheng";
//            }
//            $i=0;
//
//            foreach($fare_info as $v){
//                $data[$i]['zhuyuan_id'] = $v['zhuyuan_id'];
//                $data[$i]['fare_level'] = $v['fare_level'];
//                $data[$i]['high_temperature'] = $v['temperature'];
//                $data[$i]['zhuyuan_id'] = $zhuyuan_info[$v['zhuyuan_id']]['zhuyuan_id'];
//                $data[$i]['xingming'] = $zhuyuan_info[$v['zhuyuan_id']]['xingming'];
//                $data[$i]['nianling'] = $zhuyuan_info[$v['zhuyuan_id']]['nianling'];
//                $data[$i]['xingbie'] = $zhuyuan_info[$v['zhuyuan_id']]['xingbie'];
//                $data[$i]['bingchuang_hao'] = $zhuyuan_info[$v['zhuyuan_id']]['bingchuang_hao'];
//                //最后一次体温测量结果
//                $last_shuju = $PatientBasicModel->getLastShuju($yiyuan_id,$v['zhuyuan_id'],$zhuyuan_tizheng);
//                if($last_shuju){
//                    $data[$i]['last_temperature'] = $last_shuju['jiancha_value'];
//                    $data[$i]['jiancha_time'] = $last_shuju['jiancha_time'];
//                }else{
//                    $data[$i]['last_temperature'] = "";
//                    $data[$i]['jiancha_time'] = "";
//                }
//                $i++;
//            }
//        }else{
//            $result["state"] = "0";
//            $result["response_info"] = "暂无数据";
//        }
//        $result["result"] = $data;
//        $result["error"] = 200;
//        $result["msg"] = "";
//        Utility::apiJsonResultExtends($result);
//    }




}